|
|
|
|
|
|
|
This process is simple, and the file can be edited by anyoneeven if that person doesn't have a database tool. For example, a secretary could maintain this file on a network server, and the application could copy the most recent version at startup. Of course, if you are using a database anyway, you might want to go ahead and place the names of the sales force in a table. However, a standard text file could still be used for importing into the table. |
|
|
|
|
|
|
|
|
|
The concept of a shared network file also applies to databases and other documents, which may be useful if everyone on your network uses the same desktop application programs.  |
|
|
|
|
|
|
|
|
|
|
Reading from a Sequential Text File |
|
|
|
|
|
|
|
|
Now that you know how easily you can create a sequential text file, you're ready to write some code to read information from the file. One easy way to process a sequential text file is to read it a line at a time. For the salesperson example described in the preceding section, the steps used for filling up the list box are very straightforward: |
|
|
|
|
|
|
|
|
1. Open the file for input. |
|
|
|
|
|
|
|
|
2. Read a line from the file and store it in a variable. |
|
|
|
|
|
|
|
|
3. Add the contents of the variable to the list box. |
|
|
|
|
|
|
|
|
4. Repeat steps 2 and 3 for each line in the file. |
|
|
|
|
|
|
|
|
The code for filling up the list box, which is discussed in the following sections, is shown in Listing 21.1. |
|
|
|
|
|
|
|
|
Listing 21.1 LISTFILL.ZIPFilling a List Box from a Text File |
|
|
|
|
|
|
|
|
Sub FillListBox()
Dim sTemp As String
lstPeople.Clear
Open "C:\DATA\PEOPLE.TXT" For Input As #1
While Not EOF(1)
Line Input #1, sTemp
LstPeople.AddItem sTemp
Wend
Close #1
End Sub |
|
|
|
|
|
|
|
|
Now, examine the code a little closer. First, before you read or write information, you must open the file with the Open statement. The Open statement associates the actual filename (PEOPLE.TXT in the example) with a file number. A file number is an integer value used to identify the file to other Visual Basic code: |
|
|
|
|
|
|
|
|
Open "C:\DATA\PEOPLE.TXT" For Input As #1 |
|
|
|
|
|